package com.canaan.business.service.inOutStock.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.uuid.UUID;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.inOutStock.AdjustLocationBillDetailMapper;
import com.canaan.business.domain.inOutStock.AdjustLocationBillDetail;
import com.canaan.business.service.inOutStock.IAdjustLocationBillDetailService;
import com.canaan.common.annotation.TableName;
/**
 * 调库单明细Service业务层处理
 *
 * @author canaan
 * @date 2025-03-29
 */
@Service
public class AdjustLocationBillDetailServiceImpl implements IAdjustLocationBillDetailService {
    @Resource
    private AdjustLocationBillDetailMapper adjustLocationBillDetailMapper;

    /**
     * 查询调库单明细
     *
     * @param id 调库单明细主键
     * @return 调库单明细
     */
    @Override
    public AdjustLocationBillDetail selectAdjustLocationBillDetailById(Long id) {
        return adjustLocationBillDetailMapper.selectAdjustLocationBillDetailById(id);
    }

    /**
     * 查询调库单明细列表
     *
     * @param adjustLocationBillDetail 调库单明细
     * @return 调库单明细
     */
    @Override
    public List<AdjustLocationBillDetail> selectAdjustLocationBillDetailList(AdjustLocationBillDetail adjustLocationBillDetail) {
        return adjustLocationBillDetailMapper.selectAdjustLocationBillDetailList(adjustLocationBillDetail);
    }

    /**
     * 新增调库单明细
     *
     * @param adjustLocationBillDetail 调库单明细
     * @return 结果
     */
    @Override
    public int insertAdjustLocationBillDetail(AdjustLocationBillDetail adjustLocationBillDetail) {
        if(StringUtils.isBlank(adjustLocationBillDetail.getNo())){
            adjustLocationBillDetail.setNo(TableNoUtil.getNo(AdjustLocationBillDetail.class));
        }
        adjustLocationBillDetail.setCreateBy(SecurityUtils.getUsername());
        adjustLocationBillDetail.setCreateTime(DateUtils.getNowDate());
        return adjustLocationBillDetailMapper.insertAdjustLocationBillDetail(adjustLocationBillDetail);
    }

    /**
     * 批量新增调库单明细
     *
     * @param adjustLocationBillDetailList 调库单明细列表
     * @return 结果
     */
    @Override
    public int batchInsertAdjustLocationBillDetail(List<AdjustLocationBillDetail> adjustLocationBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (AdjustLocationBillDetail adjustLocationBillDetail : adjustLocationBillDetailList){
            if(StringUtils.isBlank(adjustLocationBillDetail.getNo())){
                adjustLocationBillDetail.setNo(TableNoUtil.getNo(AdjustLocationBillDetail.class));
            }
            adjustLocationBillDetail.setCreateBy(username);
            adjustLocationBillDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<adjustLocationBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > adjustLocationBillDetailList.size()) {
                endIndex = adjustLocationBillDetailList.size();
            }
            insertNum = insertNum + adjustLocationBillDetailMapper.batchInsertAdjustLocationBillDetail(adjustLocationBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改调库单明细
     *
     * @param adjustLocationBillDetail 调库单明细
     * @return 结果
     */
    @Override
    public int updateAdjustLocationBillDetail(AdjustLocationBillDetail adjustLocationBillDetail) {
        adjustLocationBillDetail.setUpdateBy(SecurityUtils.getUsername());
        adjustLocationBillDetail.setUpdateTime(DateUtils.getNowDate());
        return adjustLocationBillDetailMapper.updateAdjustLocationBillDetail(adjustLocationBillDetail);
    }

    /**
     * 批量修改调库单明细
     *
     * @param adjustLocationBillDetailList 调库单明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateAdjustLocationBillDetail(List<AdjustLocationBillDetail> adjustLocationBillDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (AdjustLocationBillDetail adjustLocationBillDetail : adjustLocationBillDetailList){
            adjustLocationBillDetail.setUpdateBy(username);
            adjustLocationBillDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<adjustLocationBillDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > adjustLocationBillDetailList.size()) {
                endIndex = adjustLocationBillDetailList.size();
            }
            updateNum = updateNum + adjustLocationBillDetailMapper.batchUpdateAdjustLocationBillDetail(adjustLocationBillDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除调库单明细
     *
     * @param ids 需要删除的调库单明细主键集合
     * @return 结果
     */
    @Override
    public int deleteAdjustLocationBillDetailByIds(Long[] ids) {
        return adjustLocationBillDetailMapper.deleteAdjustLocationBillDetailByIds(ids);
    }

    /**
     * 删除调库单明细信息
     *
     * @param id 调库单明细主键
     * @return 结果
     */
    @Override
    public int deleteAdjustLocationBillDetailById(Long id) {
        return adjustLocationBillDetailMapper.deleteAdjustLocationBillDetailById(id);
    }

    /**
     * 查询调库单明细
     *
     * @param no 编号
     * @return 调库单明细
     */
    @Override
    public AdjustLocationBillDetail selectAdjustLocationBillDetailByNo(String no) {
        return adjustLocationBillDetailMapper.selectAdjustLocationBillDetailByNo(no);
    }

    /**
     * 批量删除调库单明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteAdjustLocationBillDetailByNos(String[] nos) {
        return adjustLocationBillDetailMapper.deleteAdjustLocationBillDetailByNos(nos);
    }

    /**
     * 删除调库单明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteAdjustLocationBillDetailByNo(String no) {
        return adjustLocationBillDetailMapper.deleteAdjustLocationBillDetailByNo(no);
    }
}
